package com.anyonavinfo.cpaddemo.data.hostinfo.source.service;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONStringer;

import java.util.ArrayList;

/**
 * Created by Drive on 2017/1/18.
 */

public class E3HWServiceHelper {
    private static ArrayList<ServiceObserver> observerArrayList;
    private static E3HWServiceHelper serviceHelper;
    String TAG = this.getClass().getSimpleName();
    OnJsonOutput onJsonOutput = null;

    public static final int SET_WIFIPASSWORD = 0x0005;
    public static final int CHANGE_RADIO_MODE=0x1001;
    public static final int CHANGE_MEDIA_MODE =0x1002;
    public static final int CHANGE_TO_FM_CHANNEL=0x1003;
    public static final int CHANGE_TO_AM_CHANNEL=0x1004;
    public static final int SEEK_UP=0x1005;
    public static final int SEEK_DOWN= 0x1006;
    public static final int INIT_RADIOINFO= 0x1007;
    public static final int PAD_RECONNECTED= 0x1008;
    public static final int STOP_SEARCH_STATION= 0x1009;
    public static final int SEEK_STATION= 0x1010;
<<<<<<< HEAD
    public static final int GET_PADCONNECTION= 0x1011;
    public static final int SET_VOLUME = 0x1012;
=======
    public static final int  GET_PADCONNECTION= 0x1011;
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232

    private E3HWServiceHelper() {
        observerArrayList = new ArrayList<ServiceObserver>();
    }
    public static E3HWServiceHelper getServiceHelperInstance(){
        if(serviceHelper==null){
            synchronized (E3HWServiceHelper.class) {
                if (serviceHelper == null) {
                    serviceHelper = new E3HWServiceHelper();
                }
            }
        }
        return serviceHelper;
    }

<<<<<<< HEAD
    public void ExecCommand(int volumeValue){
        String CtrlString = "", ParaString = "";
        String vol = volumeValue + "";
        try {
            ParaString = new JSONStringer().object().key("VOL").value(vol)
                    .endObject().toString();
            CtrlString = new JSONStringer().object().key("RADIOSETVOL")
                    .value(ParaString).endObject().toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OutJson(CtrlString);
    }

=======
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
    //发送修改wifi密码命令
    public void ExecCommand(int command, String pw){
        String CtrlString = "", ParaString = "";
        switch (command) {
            case SET_WIFIPASSWORD:
                try {
                    ParaString = new JSONStringer().object().key("wifiPassword")
                            .value(pw).endObject().toString();
                    CtrlString = new JSONStringer().object().key("SETWIFIPASSOWRD")
                            .value(ParaString).endObject().toString();
                    OutJson(CtrlString);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case GET_PADCONNECTION:
                try {
                    ParaString ="";
                    CtrlString = new JSONStringer().object().key("GETPADCONNECTION")
                            .value(ParaString).endObject().toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OutJson(CtrlString);
                break;
            default:
                break;
        }
    }

    /* 接收主程序的收音机命令 */
    public void ExecCommand(int... command) {
        String CtrlString = "", ParaString = "";

        Log.d(TAG, "ExecCommand, Commande = " + command);

        switch (command[0]) {
            case CHANGE_RADIO_MODE:
                try {
                    ParaString = "";
                    CtrlString = new JSONStringer().object().key("ChangeToRAD")
                            .value(ParaString).endObject().toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.d(TAG, "ChangeToRAD");
                OutJson(CtrlString);
                break;
            case CHANGE_MEDIA_MODE:
                try {
                    ParaString = "";
                    CtrlString = new JSONStringer().object().key("ChangeToAUX")
                            .value(ParaString).endObject().toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.d(TAG, "Change_media_mode");
                OutJson(CtrlString);
                break;

            case INIT_RADIOINFO:

                try {
                    ParaString = "";
<<<<<<< HEAD
                    CtrlString = new JSONStringer().object().key("INITRADIOINFO")
=======
                    CtrlString = new JSONStringer().object().key("INITRADIOINFO2")
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232
                            .value(ParaString).endObject().toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OutJson(CtrlString);
                break;

            case PAD_RECONNECTED:

                try {
                    ParaString = "";
                    CtrlString = new JSONStringer().object().key("PADRECONNECTED")
                            .value(ParaString).endObject().toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OutJson(CtrlString);
                break;

            case STOP_SEARCH_STATION:
                try {
                    ParaString ="";
                    CtrlString = new JSONStringer().object().key("STOPSEARCHSTATION")
                            .value(ParaString).endObject().toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OutJson(CtrlString);
                break;
            case SEEK_UP:
                try {
                    ParaString ="";
                    CtrlString = new JSONStringer().object().key("SEEKUP")
                            .value(ParaString).endObject().toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OutJson(CtrlString);
                break;
            case SEEK_DOWN:
                try {
                    ParaString ="";
                    CtrlString = new JSONStringer().object().key("SEEKDOWN")
                            .value(ParaString).endObject().toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OutJson(CtrlString);
                break;

            case SEEK_STATION:
                String freq = command[1] + "";
                try {
                    ParaString =""+freq;
                    CtrlString = new JSONStringer().object().key("SEEKSTATION")
                            .value(ParaString).endObject().toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OutJson(CtrlString);
                break;

            case GET_PADCONNECTION:
                try {
                    ParaString ="";
                    CtrlString = new JSONStringer().object().key("GETPADCONNECTION")
                            .value(ParaString).endObject().toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OutJson(CtrlString);
                break;
            case CHANGE_TO_FM_CHANNEL:
                try {
                    ParaString ="";
                    CtrlString = new JSONStringer().object().key("CHANGETOFMCHANNEL")
                            .value(ParaString).endObject().toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OutJson(CtrlString);
                break;
            case CHANGE_TO_AM_CHANNEL:
                try {
                    ParaString ="";
                    CtrlString = new JSONStringer().object().key("CHANGETOAMCHANNEL")
                            .value(ParaString).endObject().toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OutJson(CtrlString);
                break;
            default:
                break;
        }

    }

    /* 通过主程序来发送给底层SOCKET数据 */
    private void OutJson(String jsonstr) {
        if (this.onJsonOutput != null) {
            this.onJsonOutput.outputjson(jsonstr);
        } else {
            Log.e(TAG, "Error Json String, null! ");
        }

    }

    public void setOnJsonOutput(OnJsonOutput onJsonOutput) {
        this.onJsonOutput = onJsonOutput;
    }

    public interface OnJsonOutput {
       void outputjson(String jsonstr);
    }


    public static void registerObserver(ServiceObserver observer) {
        if (observerArrayList != null && !observerArrayList.contains(observer))
            observerArrayList.add(observer);
    }
    public static void unregisterObserver(ServiceObserver observer){
        if(observerArrayList!=null&&observerArrayList.contains(observer))
            observerArrayList.remove(observer);
    }

    public static void unregisterAllOberver(){
        for(ServiceObserver observer:observerArrayList)
            observerArrayList.remove(observer);
    }

    public void updateServiceState(String json){
        for(ServiceObserver observer:observerArrayList)
            observer.update(json);
    }

    public interface ServiceObserver {
        void update(String json);
    }
}
